package com.fu.consumer.controller;

import com.fu.core.common.BusinessException;
import com.fu.core.common.ExceptionCode;
import com.fu.core.common.JsonResult;
import com.fu.core.pojo.Address;
import com.fu.core.pojo.Order;
import com.fu.core.pojo.OrderSku;
import com.fu.core.util.JsonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 付龙江
 * @date 2022/1/29 14:46
 */
@RestController
@RequestMapping("/order")
@CrossOrigin
@Api(tags = "前台订单相关接口")
public class OrderController {


    @PostMapping("/chooseAddress")
    @ApiOperation(value = "选择收货地 地址")
    public Map<String,Object> chooseAddress(){
        List<Address> list = new ArrayList<>();
        for (int i = 0; i < 8; i++) {
            Address address = new Address(1,"四川省成都市双流区","张三","13440154744","华府大道金茂光明城市",1);
            list.add(address);
        }
        JsonResult jsonResult = new JsonResult();
        jsonResult.setField("code",200);
        jsonResult.setField("data",list);
        return jsonResult.getMap();
    }

    @PostMapping("/updateAddress")
    @ApiOperation(value = "编辑地址")
    public Map<String,Object> updateAddress(Address address){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setField("code",200);
        jsonResult.setMsg("编辑成功");
        return jsonResult.getMap();
    }


    @PostMapping("/addAddress")
    @ApiOperation(value = "添加地址")
    @ApiImplicitParam(name = "addressId", value = "添加时这个id不填",
            required = false, dataType = "Integer")
    public Map<String,Object> addAddress(Address address){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setField("code",200);
        jsonResult.setMsg("添加地址");
        return jsonResult.getMap();
    }

    @PostMapping("/deleteAddress")
    @ApiOperation(value = "删除地址")
    @ApiImplicitParam(name = "addressId", value = "地址的id号",
            required = false, dataType = "Integer")
    public Map<String,Object> addAddress(Integer addressId){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setField("code",200);
        jsonResult.setMsg("删除地址");
        return jsonResult.getMap();
    }


    @PostMapping("/payMoney")
    @ApiOperation(value = "确认支付")
    @ApiImplicitParam(name = "orderId", value = "订单id",
            required = false, dataType = "Integer")
    public Map<String,Object> payMoney(Integer orderId){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setField("code",200);
        jsonResult.setMsg("占时还没发弄，后期弄了沙箱会有个跳转页面，扫码付钱");
        return jsonResult.getMap();
    }



    @PostMapping("/submitOrder")
    @ApiOperation(value = "确认支付")
    @ApiImplicitParam(name = "map", value = "传值方式请联管理员,提交成功，我会把订单id发给你，方便后面做支付",
            required = true, dataType = "Map")
    public Map<String,Object> submitOrder(@RequestBody Map<String,Object> map)throws Exception{
        if (map == null){
            throw  new BusinessException(ExceptionCode.NO_MATCHING_RESULT);
        }
        Object o =map.get("order");
        Order order = null;
        if (o!=null){
            order = JsonUtil.stringToObject(JsonUtil.objectToString(o), Order.class);
        }
        List orderSkus = (List)map.get("orderSkus");
        List<OrderSku> orderSkuList = new ArrayList<>();
        if (orderSkus!=null && orderSkus.size()>0){
            for (Object skus : orderSkus) {
                OrderSku orderSku = JsonUtil.stringToObject(JsonUtil.objectToString(skus), OrderSku.class);
                orderSkuList.add(orderSku);
            }
        }
        System.out.println(order);
        orderSkuList.forEach(System.out::println);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setField("code",200);
        jsonResult.setField("orderId",1);
        return jsonResult.getMap();
    }




}
